home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12431 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  53 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: mem allocation in function
  5. Date: Sat, 30 Mar 96 21:27:25 GMT
  6. Organization: none
  7. Message-ID: <828221245snz@genesis.demon.co.uk>
  8. References: <4je0qv$heq@salomon.zfe.siemens.de>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4je0qv$heq@salomon.zfe.siemens.de>
  15.            rainer@nil.zfe.siemens.de "Rainer Wartha" writes:
  16.  
  17. >
  18. >Hello,
  19. >
  20. >is it conforming to ANSI (allowed) to allocate in 
  21. >a Function Memory for a Pointer (defined external)
  22.  
  23. Do you mean assign the return value of malloc to a local pointer variable
  24. in your function?
  25.  
  26. >and use it after returning from the function
  27. >(memory not freed, free shortly before exit)
  28.  
  29. malloc'd memory is valid until it is free'd. There is no problem in returning
  30. a pointer to such memory from a function. What you can't sensibly do is
  31. return the address of a local variable in a function e.g.
  32.  
  33. RTYPE foo(void)
  34.  
  35. {
  36.      char *ptr = malloc(100);
  37.  
  38. /* Here you can */
  39.  
  40.      return ptr;
  41.  
  42. /* But you can't */
  43.  
  44.      return &ptr;
  45.  
  46. }
  47.  
  48. -- 
  49. -----------------------------------------
  50. Lawrence Kirby | fred@genesis.demon.co.uk
  51. Wilts, England | 70734.126@compuserve.com
  52. -----------------------------------------
  53.